#pragma once
#include <iostream>
#include<vector>
#include <utility>
#include<functional>
namespace bit
{
    template <class T>
    class less
    {
    public:
        bool operator()(const T& x, const T& y) {
            return x < y;
        }
    };
    template <class T>
    class greater
    {
    public:
        bool operator()(const T& x, const T& y) {
            return x > y;
        }
    };
    template <class T, class Container = std::vector<T>, class Compare = less<T> >
    class priority_queue
    {
    public:
        //typedef std::vector<T>::iterator iterator; 
        void Adjustup(Container& c)
        {
            int child = c.size() - 1;
            int parent = (child - 1) / 2;
            auto it = c.begin();
            while (parent >= 0 && comp(*(it + parent), *(it + child))) {    
                std::swap(*(it + child), *(it + parent));
                child = parent;
                parent = (child - 1) / 2;
            }
        }
        void Adjustdown(Container& c) {
            int parent = 0;
            int child = parent * 2 + 1;
            auto it = c.begin();
            while (child < c.size()) {
                if (child + 1 < c.size() && comp(*(it + child), *(it + child + 1))) { 
                    child++;
                }
                if (comp(*(it + parent), *(it + child))) {
                    std::swap(*(it + child), *(it + parent));
                    parent = child;
                    child = parent * 2 + 1;
                }
                else {
                    break;
                }
            }
        }
        priority_queue()
        {   }
        template <class InputIterator>
        priority_queue(InputIterator first, InputIterator last)
            :c(first, last)
        {
            for (int i = (c.size() - 1) / 2; i >= 0; i--) {
                Adjustdown(c);
            }
        }
        bool empty() const
        {
            return c.empty();
        }
        size_t size() const
        {
            return c.size();
        }
        const T& top() const
        {
            return c.front();
        }
        void push(const T& x)
        {
            c.push_back(x);
            Adjustup(c);
        }
        void pop()
        {
            std::swap(c.front(), c.back());
            c.pop_back();
            Adjustdown(c);
        }
    private:
        Container c;
        Compare comp;
    };
    void test()
    {
        priority_queue<int> q;
        q.push(1);
        q.push(2);
        q.push(3);
        q.push(4);
        while (!q.empty()) {
            std::cout << q.top() << " ";
            q.pop();
        }
        std::cout << std::endl << std::endl;

        std::vector<int> v;
        v.push_back(8);
        v.push_back(6);
        v.push_back(9);
        v.push_back(2);
        priority_queue<int, std::vector<int>, greater<int>> q2(v.begin(), v.end());
        while (!q2.empty()) {
            std::cout << q2.top() << " ";
            q2.pop();
        }
        std::cout << std::endl;
    }
}